Basic Operations with Viem
The QuickNode SDK uses viem to make RPC calls to your QuickNode endpoint with the SDK. All basic operations supported by viem's Public Client such as retrieving block numbers, transactions, and reading from smart contracts are available on the client
property of the Core
class.
Example: Get Block Number
Notice how we are calling getBlockNumber()
directly on the client
property:
import { Core } from '@quicknode/sdk'
const core = new Core({endpointUrl: "https://docs-demo.quiknode.pro/",})
core.client.getBlockNumber()
Example: Read Contract
With viem, you can also interact with a smart contract using the readContract function.
import { Core } from '@quicknode/sdk';
const myContractAbi = {
address: '0x2106C00Ac7dA0A3430aE667879139E832307AeAa',
abi: [
// ...
{
inputs: [],
name: 'name',
outputs: [{ name: '', type: 'string' }],
stateMutability: 'view',
type: 'function',
},
]
}
const core = new Core({
endpointUrl: "https://my-endpoint-name.quiknode.pro/myauthtoken/",
});
core.client.readContract({
...myContractAbi,
functionName: "name",
}).then(name => console.log(name));
Additional Functions
Additional functionality outside of the basic operations provided by the Public Client, is available through Viem's utilities. See the official Viem documentation for a complete list of utilities.
How to Import Viem Directly
To implement any of the utilities, you will simply need to import viem
directly in your code with:
import { viem } from "@quicknode/sdk"
Note that you will not need to install it as a direct dependency.
Example: Decode a Hex Value to a String
Here is an example of the fromHex
utility being used:
import { viem } from "@quicknode/sdk"
const response = viem.fromHex('0x48656c6c6f20776f726c642e', 'string');
How to Override a Chain
Lastly, a chain will be automatically determined from your endpoint URL, but If you would like to manually provide a chain to the Core
class, you will need to pass in a chain from viem/chains
. This can be helpful when using domain masking.
Note that you will need to install viem as a direct dependency to import a chain from it. Here is an example of how you would then use it in your code:
import { Core } from '@quicknode/sdk';
import { polygon } from 'viem/chains';
const core = new Core({
endpointUrl: 'https://mycustomdomain.example.com/',
chain: polygon,
});
See Viem's installation documentation for more information.